﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;

namespace HostingDomain3
{
    public partial class Home1 : System.Web.UI.Page
    {

        //Call class

        Hosting.Class.ArticleGroups GroupArticles = new Hosting.Class.ArticleGroups();
        Hosting.Class.Articles articles = new Hosting.Class.Articles();

        protected void Page_Load(object sender, EventArgs e)
        {
            GroupAticles();
            FillArticleToListView();  
        }
         //show articles group machine
        public void FillArticleToListView() 
        {
            Hosting.Class.Articles articles = new Hosting.Class.Articles();
            var a = articles.getAll();
            foreach(var i in a)
            {
                String content = i.AContent;
                String AfterSplit = content.SplitString(30);
                
            }
            ListView1.DataSource = a;
            ListView1.DataBind(); 
        }

        

        //lable groupArticles
        public void GroupAticles() 
        {
            var a = GroupArticles.getAll("1");

            ListviewGA.DataSource = a;
            ListviewGA.DataBind();
            
        }

        protected void ListView1_PreRender(object sender, EventArgs e)
        {
            GroupAticles();
            FillArticleToListView();   
        }

        

        //tag a show articles onclick by group

        //protected void lblAGroupName_Click(object sender, EventArgs e)
        //{
        //    ArticlesShowSuperMachine();
        //}

        //protected void lblAgroupName2_Click(object sender, EventArgs e)
        //{
        //    ArticlesLinuxMachine();
        //}

        //protected void ShowAll_Click(object sender, EventArgs e)
        //{
        //    ArticlesShowAll();
        //}

        
    }

    //Class cắt chuỗi

    public static class StringExtensions
    {
        /// <summary>
        /// Trả về phần đầu của chuỗi và đảm bảo đủ từ
        /// </summary>
        /// <param name="s"></param>
        /// <param name="length">Số ký tự tối đa được cắt</param>
        /// <returns>String</returns>
        public static string SplitString(this string s, int length)
        {
            //if (String.IsNullOrEmpty(s))
            //    throw new ArgumentNullException(s);
            var words = s.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);
            //if (words[0].Length > length)
            //    throw new ArgumentException("From the first cut to longer chain");
            var sb = new StringBuilder();
            foreach (var word in words)
            {
                if ((sb + word).Length > length)
                    return string.Format("{0}...", sb.ToString().TrimEnd(' '));
                sb.Append(word + " ");
            }
            return string.Format("{0}...", sb.ToString().TrimEnd(' '));
        }

        
    }
    }
